package org.framework.app.web.entity;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.framework.app.exception.DataCacheException;


/**
 * Title: CRM <br>
 * Description: <br>
 * Date: 2007-7-20 <br>
 * Copyright (c) 2007 CATTSoft <br>
 * 
 * @author caoyunliang
 */
/*
 * 方法1，将HashMap或TreeMap换成LinkedHashMap，LinkedHashMap按照put的顺序进行访问 。性能会比HashMap稍低
 * 方法2，TreeMap具有构造函数TreeMap(Comparator),按照作为key的对象的指定属性排序。
 */
public class DataCache {
	private static Log log = LogFactory.getLog(DataCache.class);

	public DataCache() {
	}

	private static HashMap allHashMaps = new HashMap();

	private static Class clazz = DataCache.class;

	/* 用来缓存的HashMap变量名 */
	public static final String AREA = "area";

	public static final String SYS_CONFIG = "sysConfig";

	public static final String REGION = "region";

	public static final String PROD_SPEC_CAT = "prodSpecCat";

	public static final String PROD_CAT = "prodCat";

	public static final String PROD_CAT_T = "prodCat_t";

	/* 服务开通的产品对应到crm的通讯服务 */
	public static final String COMM_SERV_SPEC = "prod";
	public static final String SUB_SERV_SPEC = "subprod";
	/* 产品树 */
	public static final String PRODUCT_T = "prod_t";

	/* 服务开通的客户服务对应crm的服务动作 */
	public static final String SERV_ACT = "changeServSpec";

	/* 本地网 */
	public static final String LOCAL_NET = "localNet";

	/* 本地网树 */
	public static final String LOCAL_NET_T = "localNet_t";

	/* 工区 */
	public static final String WORK_AREA = "workArea";

	public static final String EXCH = "exch";

	public static final String STEP = "step";
	
	public static final String EIS = "eis";
	
	public static final String GEOREGION = "GEO_REGION";

	public static final String STATUS = "status";

	public static final String GUOUP_TYPE = "groupType";

	public static final String WORK_TYPE = "workType";

	public static final String SYS_AREA_CONFIG = "sysAreaConfig";

	public static String STATUS_SO_PAUSE = "status_SO_PAUSE";// 从status表中取

	public static String STATUS_SO_ACC_NBR = "status_SO_ACC_NBR";

	public static String STATUS_BUSINESS = "status_BUSINESS";

	public static String STATUS_SO = "status_SO";

	public static String STEP_TYPE_T = "stepType_t"; // 环节类型

	public static String LOCAL_NET_AREA_EXCH = "localExch_t"; // 本地网服务区局向

	public static String BUSINESS = "business"; //
	
	public static final String TRADE = "trade";
	
	public static final String SPEC_PRPTY = "specPrpty";

	// add by 2007/10/26
	/**
	 * 服务区到营维区域级连数据集合
	 */
	public static String AREA_SERV_DEPT = "area_serv_dept_t";

	public static String SERV_DEPT = "serv_dept";

	/**
	 * 营维中心到支局级连数据集合
	 */
	public static String SERV_DEPT_BRANCH = "serv_dept_branch_t";

	public static String BRANCH = "branch";

	/**
	 * 本地网到服务区的级连数据集合localNet_t
	 */
	// public static String LOCAL_NET_AREA = "local_net_area";
	public static String LOCAL_NET_AREA = "localNet_t";
	/**
	 * 服务区到局向级连数据集合
	 */
	public static String AREA_EXCH = "area_exch_t";

	public static String CHANGE_SERV_SPEC_NO_FLAG = "changeServSpecNoFlag";
	
	public static HashMap getAllHashMaps() {
		return allHashMaps;
	}

	public static void setAllHashMaps(HashMap allHashMaps) {
		DataCache.allHashMaps = allHashMaps;
	}

	/**
	 * Clone一个HashMap
	 * 
	 * @param hash
	 *            HashMap
	 * @throws DataCacheException
	 * @return HashMap
	 */
	private static HashMap hashClone(HashMap hash) throws DataCacheException {
		if (log.isDebugEnabled()) {
			log.debug("calling DataCache.hashClone()");
		}
		if (hash == null) {
			return null;
		}
		HashMap ret = new HashMap();
		Iterator ite = hash.keySet().iterator();
		Object key = null, keyObj = null;
		Object value = null, valueObj = null;
		try {
			while (ite.hasNext()) {
				key = ite.next();

				value = hash.get(key);
				if (key.getClass().getName().startsWith("java")) {
					keyObj = key;
				} else {
					try {
						keyObj = key.getClass().newInstance();
						PropertyUtils.copyProperties(keyObj, key);
					} catch (Exception ex) {
						keyObj = key;
					}
				}

				if (value.getClass().getName().startsWith("java")) {
					valueObj = value;
				} else {
					try {
						valueObj = key.getClass().newInstance();
						PropertyUtils.copyProperties(valueObj, value);
					} catch (Exception ex) {
						valueObj = value;
					}
				}
				ret.put(keyObj, valueObj);
			}
		} catch (Exception ex) {
			throw new DataCacheException(ex);
		}
		return ret;
	}
	

	/**
	 * 取一个cache对象
	 * 
	 * @param cacheObj
	 * @throws DataCacheException
	 * @return HashMap
	 */
	public static Map getCache(String cacheObj) throws DataCacheException {

		Map allObjHash = null;
		Map result = null;
		try {
			Field field = clazz.getDeclaredField("allHashMaps");// 获取所有hashmap集合
			allObjHash = (Map) field.get(null);
			result = (Map) allObjHash.get(cacheObj);// 根据传入参数返回指定的hashmap
		} catch (Exception ex) {
			if (log.isDebugEnabled()) {
				log.debug("The cache Object[" + cacheObj + "] is not exist");
			}
			throw new DataCacheException(ex);
		}
		return result;
	}
	
	/**
	 * 取一个cache对象
	 * 
	 * @param cacheObj
	 * @throws DataCacheException
	 * @return HashMap
	 */
	public static HashMap getCacheClone(String cacheObj)
			throws DataCacheException {

		HashMap allObjHash = null;
		HashMap result = null;
		try {
			Field field = clazz.getDeclaredField("allHashMaps");// 获取所有hashmap集合
			allObjHash = (HashMap) field.get(null);
			result = (HashMap) allObjHash.get(cacheObj);// 根据传入参数返回指定的hashmap
		} catch (Exception ex) {
			if (log.isDebugEnabled()) {
				log.debug("The cache Object[" + cacheObj + "] is not exist");
			}
			throw new DataCacheException(ex);
		}
		return hashClone(result);
	}

	/**
	 * 取一个cache对象，使用LabelValueBean重新封装
	 * 
	 * @param cacheObj
	 *            String 目标cache
	 * @throws DataCacheException
	 * @return HashMap
	 */
//	public static List getListForOption(String cacheObj)
//			throws DataCacheException {
//		Map objHash = null;
//		try {
//			objHash = getCache(cacheObj);
//		} catch (Exception ex) {
//			if (log.isDebugEnabled()) {
//				log.debug("The cache Object[" + cacheObj + "] is not exist");
//			}
//			throw new DataCacheException(ex);
//		}
//		List lst = new ArrayList();
//		if (objHash != null && !objHash.isEmpty()) {
//			Iterator ite = objHash.keySet().iterator();
//			Object value, label;
//			while (ite.hasNext()) {
//				LabelValueBean bean = new LabelValueBean();
//				value = ite.next();
//				label = objHash.get(value.toString());
//				bean.setLabel(label.toString());
//				bean.setValue(value.toString());
//				lst.add(bean);
//			}
//		}
//		return lst;
//	}

	/**
	 * 根据表名和KEY值，从cache中读取Object数据
	 * 
	 * @param cacheObj
	 *            String 目标cache
	 * @param id
	 *            Object
	 * @throws DataCacheException
	 * @return Object
	 */
	public static Object getObject(String cacheObj, Object id)
			throws DataCacheException {
		Map objHash = new LinkedHashMap();
		try {
			objHash = getCache(cacheObj);
		} catch (Exception ex) {
			if (log.isDebugEnabled()) {
				log.debug("The cache Object[" + cacheObj + "] is not exist");
			}
			throw new DataCacheException(ex);
		}
		Object obj = objHash.get((String) id);
		return obj;
	}

	/**
	 * 根据表名和KEY值， 从cache中读取String数据
	 * 
	 * @param cacheObj
	 *            String 目标cache
	 * @param id
	 *            Object
	 * @throws DataCacheException
	 * @return Object
	 */
	public static String getString(String cacheObj, Object id)
			throws DataCacheException {
		Map objHash = new LinkedHashMap();
		try {
			objHash = getCache(cacheObj);
		} catch (Exception ex) {
			if (log.isDebugEnabled()) {
				log.debug("The cache Object[" + cacheObj + "] is not exist");
			}
			throw new DataCacheException(ex);
		}
		String obj = (String) objHash.get((String) id);
		if (obj == null) {
			String msg = "The id[" + id + "] is not in cache object["
					+ cacheObj + "]";
			if (log.isDebugEnabled()) {
				log.debug(msg);
			}
		}
		return obj;
	}

	/**
	 * 从cache中读取字符串数据,当找不到这个id的时候，返回空字符串("")
	 * 
	 * @param cacheObj
	 *            String 目标cache
	 * @param id
	 *            Object
	 * @throws DataCacheException
	 * @return Object
	 */
	public static String getStringNoException(String cacheObj, Object id)
			throws DataCacheException {
		Map objHash = new LinkedHashMap();
		try {
			objHash = getCache(cacheObj);
		} catch (Exception ex) {
			if (log.isDebugEnabled()) {
				log.debug("The cache Object[" + cacheObj + "] is not exist");
			}
			throw new DataCacheException(ex);
		}
		String obj = (String) objHash.get((String) id);
		if (obj == null) {
			String msg = "The id[" + id + "] is not in cache object["
					+ cacheObj + "]";
			if (log.isDebugEnabled()) {
				log.debug(msg);
			}
			return "";
		}
		return obj;
	}
	

	public static void clearAllMaps() {
		allHashMaps.clear();
	}
	
	
}